define(['jquery','api','user','text!pages/coupon/list.html','Vue'],function($,api,user){
	var vm,
	   controller = {
	   init:function(id){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('我的赢家券');
		   if(!vm){
		     this.render();
		   }else{
		     vm.show=true;
		   }
		   this.loadCoupons();
	   },
	   render:function(){
	   	   $('#pages').append('<div id="coupon"></div>');
	       require(['text!pages/coupon/list.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#coupon',
	               data:{
	               	  show:true,
	               	  showCoupon:false,
	               	  loading:true,
		              noRecord:'/static/img/none_content.png',
		              couponNone:false,
		              tab:1,
		              coupons:{}
	               },
	               methods:{
	                  switchTab:function(i){
	                  	 this.tab=i;
	                  }
	               },
	               template:tpl
	            })
	       })
	   },
	   loadCoupons:function(){
	   	   var that=this;
	       api.couponsAll(function(rs){
	       	    vm.loading=false;
	            if(rs.retcode=='0'){
	                if(rs.data){
	                   that.renderData(rs.data);
	                }else{
	                   vm.couponNone=true;
	                }
	            }
	       })
	   },
	   renderData:function(data){
	   	   var temp={
		   	      1:[],
		   	      2:[],
		   	      3:[]
		   	   },typeHash={
		   	      1:1,
		   	      0:2,
		   	      2:3
		   	   },
		   	   type,
		   	   coupon;
	   	   for(var i=0;i<data.length;i++){
	   	      coupon=data[i];
	   	      type=typeHash[coupon.status];
	   	      temp[type].push(coupon);
	   	   }
	   	   vm.coupons=temp;
	   	   vm.showCoupon=true;
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})